package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("保存地址："+addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> isDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> luw=new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getUserId,BaseContext.getCurrentId());//该用户
        luw.set(AddressBook::getIsDefault,0);
        addressBookService.update(luw);

        LambdaUpdateWrapper<AddressBook> qw=new LambdaUpdateWrapper<>();
        qw.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        qw.eq(AddressBook::getId,addressBook.getId());
        qw.set(AddressBook::getIsDefault,1);
        addressBookService.update(qw);

        return R.success("默认地址更新成功");
    }

    /**
     * 根据ID查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> queryAddressById(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,id);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> queryIsDefault(){
        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        Long id = BaseContext.getCurrentId();
        queryWrapper.eq(AddressBook::getUserId,id);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        return R.success(addressBook);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> qw=new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,id);

        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);


    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){

        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 更新
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        long id = Thread.currentThread().getId();
        log.info("更新：{},线程id：{}",addressBook.toString(),id);

        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

}
